package com.polaris.live.ui.main.live

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.polaris.live.adapter.home.FriendShipAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.DebounceHelper
import com.polaris.live.databinding.FragmentFriendFollowBinding
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.utils.LiveUtils
import com.polaris.live.utils.StartLiveProxy
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.me.FriendRoomViewModel

/**
 * FriendRoomFragment
 *
 * @author Created by 半仙 on 2024/9/20/020 14:15
 */
class FriendRoomFragment : AbstractLiveListFragment<FriendRoomViewModel, FragmentFriendFollowBinding>() {

    private val mType by lazy { arguments?.getString("type") ?: FOLLOWER }

    override val refreshView by lazy { mBinding.refresh }

    override val layoutManager by lazy { LinearLayoutManager(context) }

    override val mAdapter by lazy { FriendShipAdapter(FriendShipAdapter.CHARM, viewLifecycleOwner.lifecycleScope) }

    override val listLiveData by lazy { mViewModel.mListLiveData }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        setRefreshTime(FIVE_MINUTES)
        mViewModel.mRequestType = mType
        mBinding.refresh.setEnableRefresh(false)
        mViewModel.mPartyRoomLiveData.observe(this) { resultState ->
            parseState(resultState, {
                if (it.roomId?.isNotEmpty() == true) {
                    val liveType = UserManager.getUser().live?.liveType
                    StartLiveProxy.builder(this)
                        .recoverLive(mViewModel, liveType, it.roomId)
                } else {
                    //开启派对
                    Router.builder(RoutePath.SET_PARTY_COVER_ACTIVITY)
                        .navigation(requireActivity())
                }
            })
        }

        mViewModel.mListLiveData.observe(this) {
            mBinding.refresh.setEnableRefresh(true)
        }
    }

    override fun initData() {
        super.initData()
        mViewModel.onRefresh()
    }

    override fun initListener() {
        mAdapter.setOnItemClickListener { _, _, position ->
            DebounceHelper.singleClick {
                val item = mAdapter.getItemOrNull(position) ?: return@singleClick
                if (item is DisplayUserBean) {
                    if (item.base.userId == UserManager.getUserId()) {
                        mViewModel.getPartyRoom()
                    } else {
                        GlobalPopupManager.showCompletePopup(requireContext(), isOk = {
                            LiveUtils.invokeSpectator(requireContext(), item)
                        })
                    }
                }
            }
        }
    }

    companion object {

        const val FOLLOWER = "follower"     //关注页
        const val NEARBY = "nearby"         //附近页
        const val JOINED = "joined"         //已加入
    }
}